package com.aqie.easy.binarySearchTree;

import com.aqie.easy.structure.TreeNode;

/**
 * 中序遍历的时候直接保存相邻两个数的差
 * 783 二叉搜索树节点最小距离
 * todo
 */
public class MinDiffInBST {

    int minDiff = 100;
    int tmp1 = -100;
    int tmp2 = -100;
    public int minDiffInBST(TreeNode root) {
        // 二叉搜索树转换为数组,求 数组最小差值

        return inorder(root);
    }
    private int inorder(TreeNode r){
        if (r == null) return minDiff;
        if (r.left != null) inorder(r.left);

        tmp1 = tmp2;
        tmp2 = r.val;
        minDiff = Math.min(minDiff, tmp2 - tmp1);
        if (r.right != null) inorder(r.right);
        return minDiff;
    }

    public static void main(String[] args) {
        Object[] arr = {1564,1434,3048,1,null,null,3184};
    }
}
